from models import Inscritos
from django.contrib import admin
from types import NoneType



class InscritosAdmin(admin.ModelAdmin):
    
    ################################################################
    fieldsets = (
        ('Inscritos', {
                'fields': ('nome', 'noEvento','idade','sexo', 'telefone',
                           'paroquia','email',
                           'bairro',
                           ),
                'classes': ['wide',]
        }),
    )
    #filter_horizontal = ('userflags',)
    
    list_display = ('nome', 'noEvento', 'bairro', 'telefone', 'paroquia', 'email')
    #list_editable = ['noEvento']
    list_filter = ('noEvento','sexo')
    list_per_page = 20
    ordering = ('nome',)
    #raw_id_fields = ('company',) 
    readonly_fields = ['sexo']
    search_fields = ['nome']
    list_select_related = True

    ################################################################
    def get_readonly_fields(self, request, obj = None):
        
        try:
            
            if obj.noEvento == True:
                #ALREADY IN THE EVENT
                if 'noEvento' in self.readonly_fields:
                    #IF SU- REMOVE READONLY
                    if request.user.is_superuser:
                        self.readonly_fields.remove('noEvento')
                else:
                    #IF NOT SU- APPEND READONLY
                    if not request.user.is_superuser:
                        self.readonly_fields.append('noEvento')
            else:
                #NOT YET IN THE EVENT
                if 'noEvento' in self.readonly_fields:
                    #NOEVENTO NOT READONLY
                    self.readonly_fields.remove('noEvento')
            
            if not request.user.is_superuser:
                #IF NOT SUPERUSER    
                if 'nome' not in self.readonly_fields:
                    #NOME IS READONLY
                    self.readonly_fields.append('nome')
                if 'sexo' not in self.readonly_fields:
                    #SEXO IS READONLY
                    self.readonly_fields.append('sexo')
            else:
                #IF SU
                if 'sexo' in self.readonly_fields:
                    self.readonly_fields.remove('sexo')
                if 'nome' in self.readonly_fields:
                    self.readonly_fields.remove('nome')
                    
            return self.readonly_fields
        except AttributeError:
            #NEW INSCRITOS OBJECT
            self.readonly_fields = []
            return self.readonly_fields

#    def queryset(self, request):
#        qs = super(InscritosAdmin, self).queryset(request)
#        if 'noEvento' in self.list_editable:
#            self.list_editable.remove('noEvento')
#        return qs
  

admin.site.register(Inscritos, InscritosAdmin)